{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 13.7 \u590d\u5236\u6216\u8005\u79fb\u52a8\u6587\u4ef6\u548c\u76ee\u5f55\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8981\u590d\u5236\u6216\u79fb\u52a8\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u4f46\u662f\u53c8\u4e0d\u60f3\u8c03\u7528shell\u547d\u4ee4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "shutil \u6a21\u5757\u6709\u5f88\u591a\u4fbf\u6377\u7684\u51fd\u6570\u53ef\u4ee5\u590d\u5236\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import shutil\n\n# Copy src to dst. (cp src dst)\nshutil.copy(src, dst)\n\n# Copy files, but preserve metadata (cp -p src dst)\nshutil.copy2(src, dst)\n\n# Copy directory tree (cp -R src dst)\nshutil.copytree(src, dst)\n\n# Move src to dst (mv src dst)\nshutil.move(src, dst)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e9b\u51fd\u6570\u7684\u53c2\u6570\u90fd\u662f\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u540d\u3002\n\u5e95\u5c42\u8bed\u4e49\u6a21\u62df\u4e86\u7c7b\u4f3c\u7684Unix\u547d\u4ee4\uff0c\u5982\u4e0a\u9762\u7684\u6ce8\u91ca\u90e8\u5206\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5bf9\u4e8e\u7b26\u53f7\u94fe\u63a5\u800c\u5df2\u8fd9\u4e9b\u547d\u4ee4\u5904\u7406\u7684\u662f\u5b83\u6307\u5411\u7684\u4e1c\u897f\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u6e90\u6587\u4ef6\u662f\u4e00\u4e2a\u7b26\u53f7\u94fe\u63a5\uff0c\u90a3\u4e48\u76ee\u6807\u6587\u4ef6\u5c06\u4f1a\u662f\u7b26\u53f7\u94fe\u63a5\u6307\u5411\u7684\u6587\u4ef6\u3002\n\u5982\u679c\u4f60\u53ea\u60f3\u590d\u5236\u7b26\u53f7\u94fe\u63a5\u672c\u8eab\uff0c\u90a3\u4e48\u9700\u8981\u6307\u5b9a\u5173\u952e\u5b57\u53c2\u6570 follow_symlinks ,\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4fdd\u7559\u88ab\u590d\u5236\u76ee\u5f55\u4e2d\u7684\u7b26\u53f7\u94fe\u63a5\uff0c\u50cf\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "shutil.copytree(src, dst, symlinks=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "copytree() \u53ef\u4ee5\u8ba9\u4f60\u5728\u590d\u5236\u8fc7\u7a0b\u4e2d\u9009\u62e9\u6027\u7684\u5ffd\u7565\u67d0\u4e9b\u6587\u4ef6\u6216\u76ee\u5f55\u3002\n\u4f60\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u5ffd\u7565\u51fd\u6570\uff0c\u63a5\u53d7\u4e00\u4e2a\u76ee\u5f55\u540d\u548c\u6587\u4ef6\u540d\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u8fd4\u56de\u4e00\u4e2a\u5ffd\u7565\u7684\u540d\u79f0\u5217\u8868\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def ignore_pyc_files(dirname, filenames):\n    return [name in filenames if name.endswith('.pyc')]\n\nshutil.copytree(src, dst, ignore=ignore_pyc_files)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e\u5ffd\u7565\u67d0\u79cd\u6a21\u5f0f\u7684\u6587\u4ef6\u540d\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u56e0\u6b64\u4e00\u4e2a\u4fbf\u6377\u7684\u51fd\u6570 ignore_patterns() \u5df2\u7ecf\u5305\u542b\u5728\u91cc\u9762\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "shutil.copytree(src, dst, ignore=shutil.ignore_patterns('*~', '*.pyc'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 shutil \u590d\u5236\u6587\u4ef6\u548c\u76ee\u5f55\u4e5f\u5fd2\u7b80\u5355\u4e86\u70b9\u5427\u3002\n\u4e0d\u8fc7\uff0c\u5bf9\u4e8e\u6587\u4ef6\u5143\u6570\u636e\u4fe1\u606f\uff0ccopy2() \u8fd9\u6837\u7684\u51fd\u6570\u53ea\u80fd\u5c3d\u81ea\u5df1\u6700\u5927\u80fd\u529b\u6765\u4fdd\u7559\u5b83\u3002\n\u8bbf\u95ee\u65f6\u95f4\u3001\u521b\u5efa\u65f6\u95f4\u548c\u6743\u9650\u8fd9\u4e9b\u57fa\u672c\u4fe1\u606f\u4f1a\u88ab\u4fdd\u7559\uff0c\n\u4f46\u662f\u5bf9\u4e8e\u6240\u6709\u8005\u3001ACLs\u3001\u8d44\u6e90fork\u548c\u5176\u4ed6\u66f4\u6df1\u5c42\u6b21\u7684\u6587\u4ef6\u5143\u4fe1\u606f\u5c31\u8bf4\u4e0d\u51c6\u4e86\uff0c\n\u8fd9\u4e2a\u8fd8\u5f97\u4f9d\u8d56\u4e8e\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u7c7b\u578b\u548c\u7528\u6237\u6240\u62e5\u6709\u7684\u8bbf\u95ee\u6743\u9650\u3002\n\u4f60\u901a\u5e38\u4e0d\u4f1a\u53bb\u4f7f\u7528 shutil.copytree() \u51fd\u6570\u6765\u6267\u884c\u7cfb\u7edf\u5907\u4efd\u3002\n\u5f53\u5904\u7406\u6587\u4ef6\u540d\u7684\u65f6\u5019\uff0c\u6700\u597d\u4f7f\u7528 os.path \u4e2d\u7684\u51fd\u6570\u6765\u786e\u4fdd\u6700\u5927\u7684\u53ef\u79fb\u690d\u6027\uff08\u7279\u522b\u662f\u540c\u65f6\u8981\u9002\u7528\u4e8eUnix\u548cWindows\uff09\u3002\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "filename = '/Users/guido/programs/spam.py'\nimport os.path\nos.path.basename(filename)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.dirname(filename)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.split(filename)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.join('/new/dir', os.path.basename(filename))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.expanduser('~/guido/programs/spam.py')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 copytree() \u590d\u5236\u6587\u4ef6\u5939\u7684\u4e00\u4e2a\u68d8\u624b\u7684\u95ee\u9898\u662f\u5bf9\u4e8e\u9519\u8bef\u7684\u5904\u7406\u3002\n\u4f8b\u5982\uff0c\u5728\u590d\u5236\u8fc7\u7a0b\u4e2d\uff0c\u51fd\u6570\u53ef\u80fd\u4f1a\u78b0\u5230\u635f\u574f\u7684\u7b26\u53f7\u94fe\u63a5\uff0c\u56e0\u4e3a\u6743\u9650\u65e0\u6cd5\u8bbf\u95ee\u6587\u4ef6\u7684\u95ee\u9898\u7b49\u7b49\u3002\n\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6240\u6709\u78b0\u5230\u7684\u95ee\u9898\u4f1a\u88ab\u6536\u96c6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u5e76\u6253\u5305\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u5f02\u5e38\uff0c\u5230\u4e86\u6700\u540e\u518d\u629b\u51fa\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    shutil.copytree(src, dst)\nexcept shutil.Error as e:\n    for src, dst, msg in e.args[0]:\n         # src is source name\n         # dst is destination name\n         # msg is error message from exception\n         print(dst, src, msg)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u63d0\u4f9b\u5173\u952e\u5b57\u53c2\u6570 ignore_dangling_symlinks=True \uff0c\n\u8fd9\u65f6\u5019 copytree() \u4f1a\u5ffd\u7565\u6389\u65e0\u6548\u7b26\u53f7\u94fe\u63a5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6f14\u793a\u7684\u8fd9\u4e9b\u51fd\u6570\u90fd\u662f\u6700\u5e38\u89c1\u7684\u3002\u4e0d\u8fc7\uff0cshutil \u8fd8\u6709\u66f4\u591a\u7684\u548c\u590d\u5236\u6570\u636e\u76f8\u5173\u7684\u64cd\u4f5c\u3002\n\u5b83\u7684\u6587\u6863\u5f88\u503c\u5f97\u4e00\u770b\uff0c\u53c2\u8003 Python documentation"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}